package com.cjm.algorithm.leetcode;

public class MergeTwoOrderArray {
    public static void merge(int[] nums1, int m, int[] nums2, int n) {
        int k = 0;
        int j = 0;
        int[] temp = new int[m + n];
        int index = 0;
        while (index <= (m + n - 1)) {
           if (k <= m-1 && j <= n - 1) {
               int i = nums1[k];
               int x = nums2[j];
               if (i < x) {
                   temp[index] = i;
                   k++;
               } else {
                   temp[index] = x;
                   j++;
               }
               index++;
               continue;
           }
           if (k <= m - 1) {
               temp[index] = nums1[k];
               k++;
               index++;
               continue;
           }
           if (j <= n - 1) {
               temp[index] = nums2[j];
               index++;
               j++;
           }
        }

        for (int i = 0; i < temp.length; i++) {
            nums1[i] = temp[i];
        }
    }

    public static void main(String[] args) {
        int[] n1 = new int[] {1,2,3,0,0,0};
        int[] m2 = new int[] {2,5,6};
        merge(n1, 3, m2, 3);
        System.err.println("OK");
    }
}
